package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import com.qfedu.openplateform.manager.service.RechargeServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author Mr.Li
 * @date 2021/10/27 18:57
 * @className RechargeController
 * @description 充值管理控制层
 */
@RestController
@RequestMapping("/sys/recharge")
public class RechargeController {

    @Autowired
    private RechargeServcie rechargeServcie;


    @Autowired
    private CustomerService customerService;

    /**
     * 分页 并 查询所有金额
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/table")
    public TableData findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer pageSize
    ) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        System.out.println();

        // 查询所哟充值信息
        List<Recharge> rechargeList = rechargeServcie.findAll();

        // 封装用户所有值
        PageInfo<Recharge> pageInfo = new PageInfo<>(rechargeList);

        // 获取
        List<Recharge> list = pageInfo.getList();

        // 获取总数
        long total = pageInfo.getTotal();

        // 创建TableData对象
        TableData<Recharge> rechargeTableData = new TableData<>();
        rechargeTableData.setData(list);
        rechargeTableData.setCount(total);

        return rechargeTableData;
    }

    // 删除选中的充值订单
    @RequestMapping("/del")
    public AjaxMessage del(int[] ids) {

        try {
            rechargeServcie.del(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            return new AjaxMessage(false, "删除失败");
        }
    }

    // 添加充值订单
    @RequestMapping("/add")
    public AjaxMessage add(Recharge recharge) {
        try {
            Date date = new Date();
            recharge.setCreatetime(date);
            recharge.setUpdatetime(date);

            long timeMillis = System.currentTimeMillis();
            recharge.setOrderId(timeMillis);

            rechargeServcie.add(recharge);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            return new AjaxMessage(false, "添加失败");
        }
    }


    // 修改充值订单
    @RequestMapping("/update")
    public AjaxMessage update(Recharge recharge) {

        try {
            Date date = new Date();
            recharge.setCreatetime(date);
            rechargeServcie.update(recharge);
            return new AjaxMessage(true, "新增成功");
        } catch (Exception e) {
            return new AjaxMessage(false, "新增失败");
        }
    }
}
